#include "CmdDialog.h"

QString un_utf8(const char*ChineseString); 

CmdDialog::CmdDialog(const QString &cmd,QWidget* parent,const char* name)
    :ZMainWidget(parent,name)
{
  QFont fnt( font() );
  fnt.setPointSize(14);
  setFont( fnt );

	 QVBoxLayout *vLayout = new QVBoxLayout(this);
	 vLayout->setSpacing(0);
  vLayout->setMargin(0);

	 Title = new QLabel(tr("Execute command or program"), this, NULL, 0);
  setTitleBarWidget(Title);
	 //Title->setAlignment(AlignCenter);
  vLayout->addWidget(Title);
  
	QHBoxLayout *hLayout=new QHBoxLayout(this);
	vLayout->addLayout(hLayout);
	
	cmdline = new QComboBox(cmd,this);
	cmdline->insertItem(QString("%1").arg(cmd)      );
	//setCmdMenu();
	//cmdline->setText(cmd);
	cmdline->resize(214,26);
	hLayout->addWidget(cmdline,1);  //stretch
	connect(cmdline->lineEdit(),SIGNAL(returnPressed()),this,SLOT(runCmd()));
  /*
    cmdline = new QLineEdit(this);
    cmdline->setText(cmdname);
    vLayout->addWidget(cmdline);
  */
	historyButton=new ZPushButton(this);
	historyButton->setText("H");
	historyButton->resize(26,26);
	connect(historyButton,SIGNAL(clicked()),this,SLOT(showHistory()));
	hLayout->addWidget(historyButton);
	
	Outinfo = new QTextView(this, "Outinfo");
	//Outinfo->setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
	//Outinfo->setLineWidth( 1 );
	Outinfo->setTextFormat( PlainText );
	Outinfo->setUpdatesEnabled( false );
	Outinfo->ensureVisible(0,  Outinfo->contentsHeight());  
	//Outinfo->setPaper(QBrush(QColor( 0, 0, 0),NULL));
	Outinfo->setFont(fnt);
	//Outinfo->setHScrollBarMode(QScrollView::AlwaysOff);
	//Outinfo->setVScrollBarMode(QScrollView::AlwaysOff);
	vLayout->addWidget(Outinfo);
	setContentWidget( Outinfo );

	 //CST
	cst = new UTIL_CST(this,tr("Run"));
	setCSTWidget(cst);
	vLayout->addWidget(cst);

	mMiddleBtn = cst->getMidBtn();
	mLeftBtn = cst->getLeftBtn();
	mQuitBtn = cst->getRightBtn();
	QObject::connect(mQuitBtn, SIGNAL(clicked()), this, SLOT(close()));
	QObject::connect(mMiddleBtn, SIGNAL(clicked()), this, SLOT(runCmd()));
	
	input = new QPopupMenu( this );

	input->insertItem(tr("Clear"), this, SLOT(clear()),0 );
	
	mLeftBtn->setPopup(input);

  //showMaximized();
}
CmdDialog::~CmdDialog()
{
}

void CmdDialog::setCmdMenu()
{
  int count_1 = 1 ;
  QString cmd;

  SETUP_SET_Manager mgr( "FileManage.ini" , false);

  mgr.getSettingInfo("CmdDialog", "count",count_1, 0);
  mgr.setSettingInfo("CmdDialog", "count",count_1+1);

  for(int i = 1;i <= count_1; i++)
  {
	  mgr.getSettingInfo("CmdDialog",QString().sprintf( "%d",i ),cmd,"ls -l",false);
      mgr.setSettingInfo("CmdDialog",QString().sprintf( "%d",i ),cmd, false);
  }
  mgr.setSettingInfo("CmdDialog",QString().sprintf( "%d",count_1+1 ),cmdline->currentText(), false);
}

void CmdDialog::showHistory()
{
	cmdline->clear();
}

void CmdDialog::clear()
{
	 Outinfo->setText("");
}

void CmdDialog::runCmd()
{
    run(cmdline->currentText());
	//setCmdMenu();
}

void CmdDialog::run(const QString &cmd)
{
  FILE* Compileinfo;

  QString command = cmd;
  command.append(" 2>&1 ");

  if ((Compileinfo = popen( command, "r" )) != NULL)
  {
    char line[4096];
    Outinfo->append("[root] # " + cmd + "\n");
    while( true ){
      int success = fscanf( Compileinfo, "%[^\n]\n", line );
      if ( success == EOF ) break;
      Outinfo->append(QString(line).latin1());
  }
		Outinfo->setUpdatesEnabled( true );
		Outinfo->ensureVisible(0,  Outinfo->contentsHeight());
  pclose( Compileinfo );
  }
}
